package com.qk.controller;

import com.qk.commen.PageResult;
import com.qk.commen.Result;
import com.qk.dto.ClueDto;

import com.qk.dto.MarkFalselogDto;
import com.qk.entity.Clue;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
@Slf4j
@RestController
@RequestMapping("/clues")
public class ClueController {
    @Autowired
    private ClueService clueService;


    /**
     * 1.线索查询列表
     */
    @GetMapping
    public Result list(ClueDto clueDto){

        PageResult<Clue> pageResult = clueService.listClues(clueDto);
        return Result.success(pageResult);
    }
    /**
     * 2.添加线索,无逻辑
     */
    @PostMapping
    public Result add(@RequestBody Clue clue){
        log.info("添加线索: {}", clue);
        clue.setStatus(1);
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        clueService.save(clue);
    return Result.success();
    }
    /**
     * 3.线索分配,无逻辑
     */
    @PutMapping("/assign/{clueId}/{userId}")
    public Result assign(@PathVariable Integer clueId, @PathVariable Integer userId){
        log.info("线索分配:{}", clueId);
        Clue clue = new Clue();
        clue.setId(clueId);
        clue.setUserId(userId);//分配给谁
        clue.setStatus(2);//待跟进
        clue.setUpdateTime(LocalDateTime.now());
        clueService.updateById(clue);
        return Result.success();
    }
    /**
     * 4.伪线索
     * 伪线索操作，是指该线索没有意向，将其标记为伪线索，后续不再跟进。
     * 1. 根据ID更新线索的状态为 4 （伪线索）
     * 2. 添加一条跟进记录，跟进的类型type为0，表示伪线索；上图中的伪线索的原因对应于 falseReason ;
     * 备注说明对应于跟进记录 record 。（subject，level，nexttime没有值，就不需要指定）
     */
    @PutMapping("/false/{id}")
    public Result markFalseClue(@PathVariable Integer id,  @RequestBody MarkFalselogDto markFalselogDto) {
        log.info("标记为伪线索: {}", id);
        clueService.markFalseClue(id, markFalselogDto);
        return Result.success();
    }
    /**
     * 5.转商机
     */
    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id) {
        log.info("转商机: {}", id);
        clueService.toBusiness(id);
        return Result.success();
    }
    /**
     * 跟进线索,步骤一,查询回显
     *select clue by id
     */
    @GetMapping("/{id}")
    public Result getClueById(@PathVariable Integer id) {
        log.info("查询线索: {}", id);
        Clue clue = clueService.getClueById(id);
        return Result.success(clue);
    }
    /**
     * 跟进线索,步骤二,
     * 更新用户基本信息
     * 插如跟进线索跟踪记录
     */
    @PutMapping
    public Result updateClue(@RequestBody Clue clue) {
        log.info("更新线索: {}", clue);
        clueService.trackClue(clue);
        return Result.success();
    }
    /**
     * 8线索池
     */
    @GetMapping("/pool")
    public Result listPool(ClueDto clueDto) {
        log.info("查询线索池: {}", clueDto);
        PageResult<Clue> pageResult = clueService.listPool(clueDto);
        return Result.success(pageResult);
    }

}
